package com.lv.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lv.common.AjaxResult;
import com.lv.common.dto.MenuList;
import com.lv.common.untils.TokenUntil;
import com.lv.entity.Role;
import com.lv.entity.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  角色管理接口
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-06
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @GetMapping("/getAllRole/{current}/{size}")
    @PreAuthorize("hasAuthority('sys:role:manager')")
    public AjaxResult getAllRole(@PathVariable Integer current, @PathVariable Integer size, String name){//获取所有角色信息
        IPage<Role> page = roleService.getAllRole(current, size, name);
        return AjaxResult.success(page);
    }
    @GetMapping("/getRole/{id}")
    @PreAuthorize("hasAuthority('sys:role:manager')")
    public AjaxResult getRoleById(@PathVariable Long id){//获取单个角色信息
        Role role = roleService.getById(id);
        return AjaxResult.success(role);
    }
    @DeleteMapping("/deleteRole/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public AjaxResult deleteRoleById(@PathVariable Long id){//删除角色
        try {
            roleService.deleteRole(id);
        } catch (Exception e) {
            return AjaxResult.error("删除失败！");
        }
        return AjaxResult.success("删除成功！");
    }
    @PostMapping("/updateRole")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public AjaxResult updateRole(@RequestBody Role role){//修改角色信息
        boolean b = roleService.updateById(role);
        if(!b){
            return AjaxResult.error("编辑失败！");
        }
        return AjaxResult.success("编辑成功！");
    }
    @PostMapping("/saveRole")
    @PreAuthorize("hasAuthority('sys:role:save')")
    public AjaxResult saveRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        if(!save){
            return AjaxResult.error("添加失败！");
        }
        return AjaxResult.success("添加成功！");
    }

    //获取角色绑定的菜单id
    @GetMapping("/getRoleInfo/{id}")
    @PreAuthorize("hasAuthority('sys:role:menu')")
    public AjaxResult getRoleInfo(@PathVariable Long id){
        List<Long> roleInfo = roleService.getRoleInfo(id);
        Long[] menuIds = roleInfo.toArray(new Long[0]);
        return AjaxResult.success(menuIds);
    }
    //分配权限
    @PostMapping("/upRoleInfo/{id}")
    @PreAuthorize("hasAuthority('sys:role:menu')")
    public AjaxResult upRoleInfo(@PathVariable Long id, @RequestBody Long[]menuIds){
        try {
            roleService.upRoleInfo(id,menuIds);
        }catch (Exception e){
            return AjaxResult.error("修改失败");
        }
        userService.clearAuthoritiesInfo(tokenUntil.getIdByToken(request));
        return AjaxResult.success("修改成功！");
    }
    @GetMapping("/getRoleList")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public AjaxResult getRoleList(){
        List<Role> roles = roleService.list();
        return AjaxResult.success(roles);
    }

    @PostMapping("/deleteRoles")
    public AjaxResult deleteUsers(@RequestBody List<Role>list){
        boolean b = roleService.removeByIds(list);
        if(!b){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }
}

